package com.eccg.shopping.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eccg.shopping.R;
import com.eccg.shopping.utils.Store;


public class SingleCengActivity extends BaseActivity {

	private ListView floorStoreListView;
	private ArrayList<Store> StoreList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.floor_list);
		String floorNum = getIntent().getStringExtra("floorNum");
		initHeader(floorNum);
		floorStoreListView = (ListView) findViewById(R.floorList.floorlistView);
		getInfoList();
		FloorStoreAdapter adapter = new FloorStoreAdapter();
		floorStoreListView.setAdapter(adapter);
		floorStoreListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Store info = (Store) parent.getItemAtPosition(position);
				Intent intent = new Intent();
				intent.putExtra("singlestore", info);
				intent.setClass(SingleCengActivity.this, StoreActivity.class);
				startActivity(intent);
			}
			
		});
	}
	
	protected void initHeader(String floorNum) {
		// TODO Auto-generated method stub
		super.initHeader();
		titleTx.setText(floorNum);
		Button backBtn = new Button(this);
		backBtn.setText("返回");
		backBtn.setTextSize(16);
		backBtn.setTextColor(getResources().getColor(R.color.white));
		backBtn.setGravity(Gravity.CENTER);
		backBtn.setBackgroundResource(R.drawable.left_btn_bg);
		backBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				back();
			}
		});
		leftTitle.removeAllViews();
		leftTitle.addView(backBtn);
	}
	
	private void getInfoList(){
		StoreList = new ArrayList<Store>();
		Store info1 = new Store();
		info1.setStoreName("老庙黄金");
		info1.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		Store info2 = new Store();
		info2.setStoreName("周大福");
		info2.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		Store info3 = new Store();
		info3.setStoreName("Enzo");
		info3.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		Store info4 = new Store();
		info4.setStoreName("老庙黄金");
		info4.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		Store info5 = new Store();
		info5.setStoreName("老庙黄金");
		info5.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		Store info6 = new Store();
		info6.setStoreName("老庙黄金");
		info6.setStoreAbout("创始于1906年, 享誉中华老字号....");
		
		StoreList.add(info1);
		StoreList.add(info2);
		StoreList.add(info3);
		StoreList.add(info4);
		StoreList.add(info5);
		StoreList.add(info6);
	}
	
	class FloorStoreAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return StoreList.size();
		}

		@Override
		public Store getItem(int position) {
			// TODO Auto-generated method stub
			return StoreList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Store info = getItem(position);
			ViewHolder holder = null;
			if(convertView == null){
				LayoutInflater inflater = getLayoutInflater();
				convertView = inflater.inflate(R.layout.floor_store_list_item, null);
				TextView storeNameTx = (TextView) convertView.findViewById(R.floorStoreList.storeNameTx);
				TextView storeAboutTx = (TextView) convertView.findViewById(R.floorStoreList.storeAboutTx);
				ImageView storelogoImg = (ImageView) convertView.findViewById(R.floorStoreList.storelogoImg);
				holder = new ViewHolder();
				holder.storeNameTx = storeNameTx;
				holder.storeAboutTx = storeAboutTx;
				holder.storelogoImg = storelogoImg;
				convertView.setTag(holder);
			}
			
			holder = (ViewHolder) convertView.getTag();
			holder.storeNameTx.setText(info.getStoreName());
			holder.storeAboutTx.setText(info.getStoreAbout());
			holder.storelogoImg.setImageResource(R.drawable.zhoudafu);
			
			return convertView;
		}
		
		class ViewHolder{
			ImageView storelogoImg;
			TextView storeNameTx;
			TextView storeAboutTx;
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void back(){
		Intent intent = new Intent();
		intent.setClass(this, FloorListActivity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	protected int getMenuIndex() {
		// TODO Auto-generated method stub
		return DAOGOUINDEX;
	}

}
